package com.learn.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import com.learn.security.filter.JwtAuthenticationTokenFilter;
import com.learn.security.handler.AnonymousAuthenticationHandler;
import com.learn.security.handler.CustomAccessDeniedHandler;
import com.learn.security.handler.CustomAuthenticationFailureHandler;


@Configuration
@EnableWebSecurity
@EnableMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig{

    // @Autowired
    // private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private CustomAccessDeniedHandler customAccessDeniedHandler;

    @Autowired
    private AnonymousAuthenticationHandler anonymousAuthenticationHandler;

    @Autowired
    private CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
        return configuration.getAuthenticationManager();
    }
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
        http.csrf(csrf->csrf.disable());
        http.formLogin( configure -> configure
            .failureHandler(customAuthenticationFailureHandler)
        );
        http.sessionManagement(configure->
            configure.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        );
        http.authorizeHttpRequests(auth->auth
            .requestMatchers(
                "/user/login",
                "/captcha","/oauth/**",
                "/swagger-ui/**","/v3/api-docs/**"
            ).permitAll()
            .anyRequest()
            .authenticated()
        );
        /*
        http.addFilterBefore(
            jwtAuthenticationTokenFilter, 
            UsernamePasswordAuthenticationFilter.class
        );
        */
        http.exceptionHandling(configure -> configure
             .accessDeniedHandler(customAccessDeniedHandler)
            .authenticationEntryPoint(anonymousAuthenticationHandler)
        );
        return http.build();
    }

    public static void main(String[] args){
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String rawPassword = "123456";
        String encodedPassword = passwordEncoder.encode(rawPassword);
        System.out.println("Raw Password: " + encodedPassword);
    }
} 